#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>

#include "../include/filedesc.h"
#include "../include/hash.h"
#include "../include/bst.h"
#include "../include/narray.h"
#include "../include/vfs.h"
//#include "../include/vfs_errorcodes.h"

#define ERR_VFS_ADDFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"
#define ERR_VFS_ADDFILE_02 "INVALID_CHARACTER_IN_FILENAME"
#define ERR_VFS_ADDFILE_03 "FILE_ALREADY_EXISTS"
#define ERR_VFS_ADDFILE_04 "FILE_SYSTEM_FULL"   
#define ERR_VFS_ADDFILE_05 "CANNOT_OPEN_SPECIFIED_DATAFILE" 
#define ERR_VFS_ADDFILE_06 "FILE_TOO_LARGE" 
#define ERR_VFS_ADDFILE_07 "VFS_NOT_MOUNTED" 

#define ERR_VFS_LISTFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"
#define ERR_VFS_LISTFILE_01 "SOURCE_FILE_PATH_NOT_FOUND"
#define ERR_VFS_LISTFILE_02 "NOT_A_TEXT_FILE"
#define ERR_VFS_LISTFILE_03 "CANNOT_CREATE_OUTPUTFILE"
#define ERR_VFS_LISTFILE_04 "VFS_NOT_MOUNTED"

#define ERR_VFS_UPDATEFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"//mounting
#define ERR_VFS_UPDATEFILE_01 "INTERNAL_FILE_NOT_FOUND"//done
#define ERR_VFS_UPDATEFILE_02 "EXTERNAL_FILE_NOT_FOUND"//done
#define ERR_VFS_UPDATEFILE_03 "EXTERNAL_FILE_TOO_LARGE" //New Error Code:done
#define ERR_VFS_UPDATEFILE_04 "VFS_NOT_MOUNTED" //New:done

#define ERR_VFS_REMOVEFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"//mounting
// Use this error even when the given argument is a directory and not a file
#define ERR_VFS_REMOVEFILE_01 "CANNOT_FIND_SPECIFIED_FILE"
#define ERR_VFS_REMOVEFILE_02 "VFS_NOT_MOUNTED"//done

#define ERR_VFS_COPYFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"//mounting
#define ERR_VFS_COPYFILE_01 "CANNOT_FIND_SOURCEFILE"//done
#define ERR_VFS_COPYFILE_02 "CANNOT_FIND_DESTINATIONPATH"//done-
#define ERR_VFS_COPYFILE_03 "CANNOT_COPY_DIR_TO_FILE"
#define ERR_VFS_COPYFILE_04 "FILE_SYSTEM_FULL"//done
#define ERR_VFS_COPYFILE_05 "VFS_NOT_MOUNTED"//done


#define ERR_VFS_MOVEFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"
#define ERR_VFS_MOVEFILE_01 "CANNOT_FIND_SOURCEFILE"
#define ERR_VFS_MOVEFILE_02 "CANNOT_FIND_DESTINATION_PATH"
#define ERR_VFS_MOVEFILE_03 "DESTINATION_DOESNOT_HAVE_FILENAME"

#define ERR_VFS_MOVEFILE_06 "VFS_NOT_MOUNTED"

#define ERR_VFS_SEARCHFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"
//New
#define ERR_VFS_SEARCHFILE_02 "VFS_NOT_MOUNTED"


#define ERR_VFS_EXPORTFILE_00 "VFS_INSUFFICIENT_ARGUMENTS"
#define ERR_VFS_EXPORTFILE_01 "CANNOT_FIND_SOURCEFILE"
#define ERR_VFS_EXPORTFILE_02 "CANNOT_CREATE_OUTPUTFILE"
#define ERR_VFS_EXPORTFILE_03 "CANNOT_EXPORT_DIR"
#define ERR_VFS_EXPORTFILE_04 "VFS_NOT_MOUNTED"//done



FILE *fdi;
block blk; 
int size_meta_header;
//int size_of_header_1;
//int size_of_header_2;
//int max_fd;
//int no_of_fd;




char* addfiles (char *dest_dir_name, char *file_name, char *data)
{     
   int used_file_blocks,t=0,k=0,j;
   int block_num,blocks;
   
   char name[200],ch,data1[1024];
   FILE *fp;
   int len=0;    
   int total_size=size_meta_header+size_of_header_1+size_of_header_2;
   l_node *temp1;
//*********************************************************************************************************************
 if(strlen(dest_dir_name)==0 || strlen(file_name)==0 ||strlen(data)==0)
        {  strcpy(error,"addfile_FAILURE ");
           strcat(error,ERR_VFS_ADDFILE_00);
    		printf(ERR_VFS_ADDFILE_00);
   		return error;

        
        }
/********************************************************************************************************************/
       fdi=fopen(labels,"rb+");
          if(fdi==NULL){
   		strcpy(error,"addfile_FAILURE ");
    		strcat(error,ERR_VFS_ADDFILE_07);
    		printf(ERR_VFS_ADDFILE_07);
   		return error;
}
/*****************************************to check if file_name contains "/" *****************************************/

  if(strstr(file_name,"/")!=NULL)
  {  
    strcpy(error,"addfile_FAILURE ");
    strcat(error,ERR_VFS_ADDFILE_02);
    printf(ERR_VFS_ADDFILE_02);
   return error;
  }
/************************************** to check if file already exists  ********************************************/   


int n=strlen(dest_dir_name);
int i=0;
if(dest_dir_name[strlen(dest_dir_name)-1]=='/'){


dest_dir_name[strlen(dest_dir_name)-1]='\0';

}

fd *temp=(fd*)malloc(sizeof(fd));             
 strcpy(temp->file_name,file_name);
strcpy(name,dest_dir_name);
        strcat(name,"/");
    	strcat(name,file_name);
    	strcpy(temp->path,dest_dir_name);                                 

    	

 /**********************************************************************************************************************/
   	b_node *found= bst_search(bst_root,name);
    	if(found!=NULL) {
        strcpy(error,"addfile_FAILURE ");
    	strcat(error,ERR_VFS_ADDFILE_03);
    	printf(ERR_VFS_ADDFILE_03);
   	return error;

    	}
 /**********************************************************************************************************************/
   if(fdno>=maxfd){
   strcpy(error,"addfile_FAILURE  ");
   strcat(error,ERR_VFS_ADDFILE_04);
   printf(ERR_VFS_ADDFILE_04);
   return error;
   } /**********************************************************************************************************************/
   temp1=start;
   while(temp1->value==1 &&temp1->next!=NULL)
        {
         temp1=temp1->next;  
   	}
        temp1->value=1;
        blocks=temp1->no;   
   
    
 /************************************************************************************************************************/
      	fp=fopen(data,"rb");
    	if(fp==NULL){
	strcpy(error,"addfile_FAILURE ");
    	strcat(error,ERR_VFS_ADDFILE_05);
    	printf(ERR_VFS_ADDFILE_05);
   	return error;
		}
/*************************************************************************************************************************/
             
   	fseek(fp, 0L, SEEK_END);
       	int sz = ftell(fp);
       	fseek(fp, 0L, SEEK_SET);
       	if(sz>=1024){
      		strcpy(error,"addfile_FAILURE  ");
    		strcat(error,ERR_VFS_ADDFILE_06);
    		printf(ERR_VFS_ADDFILE_06);
   		return error;
            }
/**************************************************************************************************************************/
       
	//while( !feof(fp) && (len<1024))
//	{
//		fread(&ch, sizeof(char), 1, fp);
//		data1[len++] = ch;
//	}
 
fread(&blk, sz, 1, fp);	
        fclose(fp);
/*************************************************************************************************************************/
       temp->size=len;
       temp->blockx=blocks;
       temp->is_dir=0;
       fdno++;
       
       int nar=insert_node(&root,temp);                        
       int bst=bst_insert(&bst_root,temp);
       int has=insert_into_list(temp->file_name,dest_dir_name); 
/**********************************************************************************************************************************/
       
                block_num=temp->blockx;
       		int num=total_size+1024*block_num;
       		fseek(fdi,num,SEEK_SET);                                       
		 
       		

	//for(j=0; j<len; j++){
	//	blk.bsize[j] = data1[j];
	//	}
	//fwrite( &blk, sizeof(block), 1,fdi);

	fwrite( &blk, sz, 1,fdi);
printf("\n");

    fclose(fdi);
	//printf( "SUCCESS ADDFILE");
	strcpy(error,"addfile_SUCCESS");
	printf("%s\n",error);
	return error;

}

//************************************************************************************************************************************


char * listfiles(char file_path[],char output_file[])
{ 
    
       int i,d;
        int n=strlen(file_path),count=1,c,j;
        
        char path[MAX_PATH_SIZE];
        char **tokens;
        char ch,only_fname[200],abspath[200],fname[200],data[1024];
        char *name;
        FILE *fp;
//***************************************************************************************************************************
if(strlen(file_path)==0 || strlen(output_file)==0)
        {  
		strcpy(error,"listfile_FAILURE");
           strcat(error,ERR_VFS_LISTFILE_00);
    		printf(ERR_VFS_LISTFILE_00);
   		return error;

        }
//************************************************************************************************************************************
        int total_size=size_meta_header+size_of_header_1+size_of_header_2;

/*************************************to check if vfs exists*************************************************************/
     
       fdi=fopen(labels,"rb+");
         if(fdi==NULL)
   	{
   	strcpy(error,"listfile_FAILURE");
   	strcat(error,ERR_VFS_LISTFILE_04);
    	printf(ERR_VFS_LISTFILE_04);
   	return error;
   	}
/*************************************to check if source file exists*************************************************************/
	b_node *tmp=bst_search(bst_root,file_path);
	
         if(tmp==NULL){
    	strcpy(error,"listfile_FAILURE ");
    	strcat(error,ERR_VFS_LISTFILE_01);
    	printf(ERR_VFS_LISTFILE_01);
   	return error;
   	}
	
/**********************************************to check if it is a text_file****************************************************/

	strcpy(abspath,file_path);
	name=strtok(abspath,"/");
	while(name!=NULL){
		strcpy(fname,name);
		name=strtok(NULL,"/");
	}
	
        int pos=(int)strstr(fname,".txt");
        if(pos==0){
        strcpy(error,"listfile_FAILURE");
    	strcat(error,ERR_VFS_LISTFILE_02);
    	printf(ERR_VFS_LISTFILE_02);
   	return error;
   
	}
	int block_num=tmp->file_desc->blockx;
        int bbb=tmp->file_desc->size;
        
        
	fseek(fdi,total_size+(1024*(block_num)),SEEK_SET);
        fread(&blk,1024,1,fdi);
     	for(i=0;i<bbb;i++){
        	strcpy(data,blk.bsize);
        }
       	
	
	fp=fopen(output_file,"w");
	if(fp==NULL)
		{
                        strcpy(error,"listfile_FAILURE");
    			strcat(error,ERR_VFS_LISTFILE_03);
    			printf(ERR_VFS_LISTFILE_03);
   			return error;
   
			}
	else
	{ 
      
           fprintf(fp,"%s",data);
	   fclose(fp);
	}
 fclose(fdi);
	

	 memset(blk.bsize, 0, 1024);
    	strcpy(error,"listfile_SUCCESS");
    	printf(error);
   	return error;
 
}
//**************************************************************************************************************************************

char * updatefiles(char file_path[],char data_file_path[]){
	 int datalen=0,i,d;
         char append[1024],abspath[100],name[100],fname[100],file_pth[100];
         FILE *fp;
         int n=strlen(file_path),count=1,c,j;
         
         char path[MAX_PATH_SIZE];
         char **tokens;
         char ch,only_fname[200];
	int total_size=size_meta_header+size_of_header_1+size_of_header_2;

   if(strlen(data_file_path)==0 || strlen(file_path)==0)
        {  strcpy(error,"updatefile_FAILURE");
           strcat(error,ERR_VFS_ADDFILE_00);
    		printf(ERR_VFS_ADDFILE_00);
   		return error;

        
        }
/*******************************************************************************************************************************/

     fdi=fopen(labels,"rb+");
     if(fdi==NULL){
    strcpy(error,"updatefile_FAILURE  ");
    strcat(error,ERR_VFS_UPDATEFILE_04);
    printf(ERR_VFS_UPDATEFILE_04);
    return error;

      }
/*********************************************************************************************************************************/        
       b_node *tmp=bst_search(bst_root,file_path);
	printf("\n");
	
    if(tmp==NULL){
    strcpy(error,"updatefile_FAILURE ");
    strcat(error,ERR_VFS_UPDATEFILE_01);
    printf(ERR_VFS_UPDATEFILE_01);
    return error;
    
}


        int block_num=tmp->file_desc->blockx;

	fseek(fdi,total_size+(1024*block_num),SEEK_SET);
        int blk_size=tmp->file_desc->size;
         

int x=blk_size-2;
	fseek(fdi,x,SEEK_CUR);
       fp=fopen(data_file_path,"r");
	if(fp == NULL) {
    strcpy(error,"updatefile_FAILURE");
    strcat(error,ERR_VFS_UPDATEFILE_02);
    printf(ERR_VFS_UPDATEFILE_02);
    return error;
	}

               fseek(fp, 0L, SEEK_END);
       int sz = ftell(fp);
       
       if(sz>=1024){
      strcpy(error,"updatefile_FAILURE  ");
    strcat(error,ERR_VFS_UPDATEFILE_03);
    printf(ERR_VFS_UPDATEFILE_03);
    return error;
      }
fseek(fp, 0L, SEEK_SET);
		while((ch=fgetc(fp))!=EOF)
			{
				append[datalen]=ch;
				datalen=datalen+1;
                        
			}
			append[datalen]='\0';
		fwrite(&append,sizeof(char)*datalen,1,fdi);
              tmp->file_desc->size=x+datalen;

		fclose(fp);
fclose(fdi);
		 memset(blk.bsize, 0, 1024);


strcpy(error,"updatefile_SUCCESS");
printf("%s\n",error);
return error;

}
/********************************************************************************************************************************/

char* removefiles(char *file_path)
{

int i,n=strlen(file_path),count=1,c,j;
char path[MAX_PATH_SIZE],only_fname[200],only_pname[200];
char **tokens;

if(strlen(file_path)==1 )
        {  
        strcpy(error,"removefile_Failure");
           strcat(error,ERR_VFS_ADDFILE_00);
    	printf(ERR_VFS_ADDFILE_00);
   	return error;
}
FILE *f;
strcpy(path," ");
strcpy(path,file_path);

     fdi=fopen(labels,"rb+");
         if(fdi==NULL)
   {
   strcpy(error,"removefile_Failure");
    strcat(error,ERR_VFS_REMOVEFILE_02);
    printf(ERR_VFS_REMOVEFILE_02);
   return error;
   }



for(i=1;i<n;i++){
		if(path[i]=='/')
			count++;
	}
count++;
(tokens) = (char **)malloc(sizeof(char*)*count);

	i = 0;
	for(c=0;c<count;c++)
	{
		j = 0;
		tokens[c] = (char *)malloc(sizeof(char) * 50);
		while(path[i] != '/' && path[i]!='\0')
		{
			tokens[c][j] = path[i];
			i++;
			j++;
		}
		i++;
		tokens[c][j] = '\0';
	}

	tokens[0][0] = '/';
	//tokens[0][1] = '\0';

int d,e;
strcpy(only_pname," ");
strcpy(only_pname,tokens[0]);
for(e=1;e<count-2;e++){
        strcat(only_pname,tokens[e]);
        strcat(only_pname,"/");
        }
strcat(only_pname,tokens[e]);




b_node *current=bst_search(bst_root,file_path);
if(current==NULL)
{
  strcpy(error,"removefile_Failure ");
    strcat(error,ERR_VFS_REMOVEFILE_01);
    printf(ERR_VFS_REMOVEFILE_01);
   return error;

}
if((current->file_desc->is_dir==1))
{ 
  
   strcpy(error,"removefile_Failure ");
    strcat(error,ERR_VFS_REMOVEFILE_01);
    printf(ERR_VFS_REMOVEFILE_01);
   return error;
}



char narry[300];
strcpy(narry,deletenodes(&root,file_path));

//printtrees(root);
int block=current->file_desc->blockx;

l_node *temp,*temp1;
temp=start;
printf("narray : %s\n",narry);
while(temp->no!=block && temp->next!=NULL)
{
 temp=temp->next;
}
if(temp->no==block)
{
temp->value=0;
}
fdno=fdno-1;


fclose(fdi);
    strcpy(error,"removefile_SUCCESS");
    printf(error);
   return error; 
}

char* searchfiles(char *name, char *searchoutfile)
{
        FILE *fp;
        int count=0;
	struct list* temp=search_files(name);
	struct list* start=temp;
if(strlen(name)==0 || strlen(searchoutfile)==0)
        {  strcpy(error,"searchfle_FAILURE");
           strcat(error,ERR_VFS_SEARCHFILE_00);
    		printf(ERR_VFS_SEARCHFILE_00);
   		return error;

        
        }

        fdi=fopen(labels,"rb+");
    if(fdi==NULL){
    strcpy(error,"searchfle_FAILURE ");
    strcat(error,ERR_VFS_SEARCHFILE_02);
    printf(ERR_VFS_SEARCHFILE_02);
   return error;
}
	if(temp==NULL)
	{
	  printf("no entries");
	}
	else
	{
          fp=fopen(searchoutfile,"w");
           if(fp!=NULL)
             { 
               
                while(start!=NULL)
               {
                 count++;
                
		fprintf(fp,"%s\t",start->path_name);               
                 start=start->next;
               }
                
             
        fclose(fp);
		}

	}
fclose(fdi);
 strcpy(error,"searchfle_SUCCESS ");
    
    printf(error);
   return error;

}
//**********************************************************************************************************************************

char *movefiles(char *source_file_path,char* dest_file_path)
 {
   
      	int datalen=0,block_num,block_num1,block_number,flag=0,i,p,n=strlen(dest_file_path),c,j;
	char fname[200],abspath[200],path1[200],path[200];
        char *name;
        FILE *fp;
	b_node *tmp1,*tmp;
	fd *temp;
        char **tokens;
	int total_size=size_meta_header+size_of_header_1+size_of_header_2;

if(strlen(dest_file_path)==0 || strlen(source_file_path)==0)
        {  strcpy(error,"movefile_FAILURE");
           strcat(error,ERR_VFS_MOVEFILE_00);
    		//printf(ERR_VFS_MOVEFILE_00);
   		return error;

        
        }
                
           fdi=fopen(labels,"rb+");
               if(fdi==NULL){
      strcpy(error,"movefile_FAILURE  ");
      strcat(error,ERR_VFS_MOVEFILE_06);
      //printf(ERR_VFS_MOVEFILE_06);
      return error;
      }

l_node *temp1;
strcpy(path,dest_file_path);
/************************************************************************************************************************************/
int count=1;
if(n==1)
		count = 1;
	else
	{
	for(p=1;p<n;p++){
		if(path[p]=='/')
			count++;
	}
		count++;
	}
             
	(tokens) = (char **)malloc(sizeof(char*)*count);

	p = 0;
	for(c=0;c<count;c++)
	{
		j = 0;
		tokens[c] = (char *)malloc(sizeof(char) * 50);
		while(path[p] != '/' && path[p]!='\0')
		{
			tokens[c][j] = path[p];
			p++;
			j++;
		}
		p++;
		tokens[c][j] = '\0';
	}

	tokens[0][0] = '/';
	tokens[0][1] = '\0';
	

	
	
int x;
strcpy(abspath,tokens[0]);
for(x=1;x<count-2;x++){            
        strcat(abspath,tokens[x]);
        strcat(abspath,"/");
        
         
        }
strcat(abspath,tokens[x]);
       


tmp=NULL;
    tmp=bst_search(bst_root,abspath);
    if(tmp!=NULL)
    {   
      

/**************************trying to find if /home/dir exists*************************************************************************************/
      
	tmp1=bst_search(bst_root,source_file_path);
        if(tmp1==NULL){
            strcpy(error,"movefile_FAILURE  ");
         strcat(error,ERR_VFS_MOVEFILE_01);
            //printf(ERR_VFS_MOVEFILE_01);          
             return error;
         }

 	block_num=tmp1->file_desc->blockx;
         int bs=tmp1->file_desc->size;
       	fseek(fdi,total_size+(sizeof(block)*(block_num)),SEEK_SET);
	fread(&blk,bs,1,fdi);
   

       
/****************************************** done with src_file*********************************************/
       
    b_node *tmp2=bst_search(bst_root,dest_file_path);
	 if(tmp2==NULL){
            strcpy(error,"movefile_FAILURE  ");
         strcat(error,ERR_VFS_MOVEFILE_02);
            //printf(ERR_VFS_MOVEFILE_01);          
             return error;
         }
	


	





    if(tmp2!=NULL){


	if(tmp2->file_desc->is_dir==1)
	{
	strcpy(error,(error,"movefile_FAILURE  ");
         strcat(error,ERR_VFS_MOVEFILE_03);
            //printf(ERR_VFS_MOVEFILE_01);          
             return error;

}
       int blkn=tmp2->file_desc->blockx;
       fseek(fdi,total_size+(sizeof(block)*(blkn)),SEEK_SET);
	fwrite(&blk,bs,1,fdi);
        tmp2->file_desc->size=bs;
       
       }
       
/***********************************chk for dest********************************************************/
char err[200];
strcpy(err,deletenodes(&root,source_file_path));
//printf("%s\n",deletenodes(&root,source_file_path)); 
                       //insert into narray
       
fdno=fdno-1;
fclose(fdi); 

		 memset(blk.bsize, 0, 1024);
    strcpy(error,"movefile_SUCCESS");
    printf(error);
   return error;

}

else{

   strcpy(error,"movefile_FAILURE  ");
         strcat(error,ERR_VFS_MOVEFILE_02);
            //printf(ERR_VFS_MOVEFILE_02);          
             return error;
         



}
}
//**************************************************************************************************************************************
char* copyfiles(char source_file[],char dest_file[])//dest incl file_name
 {
            
        int n=strlen(dest_file),count=1,c,j,p;
        int x=0;
	int datalen=0,block_num,block_num1,block_number,flag=0,i;
	char fname[300],abspath[300],oly_pname[300],sname[200],path[300],xyz[200],name_gn[200],path1[200];
        char *name,*names,*named;
        char **tokens;
       FILE *fp,*fp1;
if(strlen(dest_file)==0 || strlen(source_file)==0)
        {  strcpy(error,"copyfile_FAILURE");
           strcat(error,ERR_VFS_ADDFILE_00);
    		printf(ERR_VFS_ADDFILE_00);
   		return error;

        
        }

        
        b_node *tmp1,*tmp;
        fdi=fopen(labels,"rb+");
               if(fdi==NULL){
    strcpy(error,"copyfile_FAILURE  ");
    strcat(error,ERR_VFS_COPYFILE_05);
    printf(ERR_VFS_COPYFILE_05);
    return error;

      }
   
 
        fd *temp;
	int total_size=size_meta_header+size_of_header_1+size_of_header_2;
	strcpy(path,dest_file);
/************************************************************************************************************************************/
if(n==1)
		count = 1;
	else
	{
	for(p=1;p<n;p++){
		if(path[p]=='/')
			count++;
	}
		count++;
	}
             
	(tokens) = (char **)malloc(sizeof(char*)*count);

	p = 0;
	for(c=0;c<count;c++)
	{
		j = 0;
		tokens[c] = (char *)malloc(sizeof(char) * 50);
		while(path[p] != '/' && path[p]!='\0')
		{
			tokens[c][j] = path[p];
			p++;
			j++;
		}
		p++;
		tokens[c][j] = '\0';
	}

	tokens[0][0] = '/';
	tokens[0][1] = '\0';
	

	
strcpy(abspath,tokens[0]);
for(x=1;x<count-2;x++){            
        strcat(abspath,tokens[x]);
        strcat(abspath,"/");

         
        }
  
        strcat(abspath,tokens[x]);

/****************************************************************************************************************************************/
    tmp=NULL;
    tmp=bst_search(bst_root,abspath);
    if(tmp!=NULL)//path exists
    {   
      

/***************************************************************************************************************************************/
      
	tmp1=bst_search(bst_root,source_file);
        if(tmp1==NULL){
            strcpy(error,"copyfile_FAILURE");
            strcat(error,ERR_VFS_COPYFILE_01);
            printf(ERR_VFS_COPYFILE_01);          
            return error;
         }


     if(tmp1->file_desc->is_dir==1){
           strcpy(error,"copyfile_FAILURE");
           strcat(error,ERR_VFS_COPYFILE_03);
           printf(ERR_VFS_COPYFILE_03);          
           return error;
         }
 
       

/***************************************************************************************************************************************/

 	block_num=tmp1->file_desc->blockx;
        int bs1=tmp1->file_desc->size;

        
	fseek(fdi,total_size+(sizeof(block)*(block_num)),SEEK_SET);

        fread(&blk,bs1,1,fdi);
       int k;

	b_node *tmp2=bst_search(bst_root,dest_file);
     if(tmp2!=NULL){
       int blkn=tmp2->file_desc->blockx;
          int bs=tmp2->file_desc->size;
       fseek(fdi,total_size+(sizeof(block)*(blkn)),SEEK_SET);
	fwrite(&blk,bs1,1,fdi);
                
       }
     else
    {

   l_node *temp1=start;
   while(temp1->value==1 &&temp1->next!=NULL)                 //to search if list is free ie if there is any space in vfs
   {
      temp1=temp1->next;
   }

   if(temp1->value==0)
   {                                                         //to change list to used
    temp1->value=1;
    int blocks=temp1->no;   

	temp=(fd*)malloc(sizeof(fd));
        strcpy(temp->path,abspath);
    

       strcpy(xyz,dest_file);

	named=strtok(xyz,"/");

	while(named!=NULL){
		strcpy(name_gn,named);
		named=strtok(NULL,"/");
	}

   
	
	strcpy(temp->file_name,name_gn);


        strcpy(path1,temp->path);
        strcat(path1,"/");
	strcat(path1,fname);    
        temp->blockx=blocks;
        temp->size=bs1;        
        temp->is_dir=0; 
        fdno++;

         int nar=insert_node(&root,temp);                        //insert into narray

       int bst=bst_insert(&bst_root,temp);//insert into bst
       
       int has=insert_into_list(temp->file_name,abspath);   //insert into hash   
       fseek(fdi,total_size+(sizeof(block)*(blocks)),SEEK_SET);
       fwrite(&blk,bs1,1,fdi);
                

}
else
{
     strcpy(error,"copyfile_FAILURE  ");
      strcat(error,ERR_VFS_COPYFILE_04);
         
        printf(ERR_VFS_COPYFILE_04);
return error;
 



}

}
    }
    else
    {
      strcpy(error,"copyfile_FAILURE  ");
      strcat(error,ERR_VFS_COPYFILE_02);
         
        printf(ERR_VFS_COPYFILE_02);
return error;
    }




fclose(fdi);
strcpy(error,"copyfile_SUCCESS");
printf( "%s\n",error);
return error;
}


char* exportfiles(char file_path[],char output_file[])
{ 
        int i,d;
        int n=strlen(file_path),count=1,c,j;
        
        char path[MAX_PATH_SIZE],*name;
        char **tokens;
        char ch,only_fname[200],abspath[200],fname[200];
        FILE *fp;
	printf("\n");
    
        int total_size=size_meta_header+size_of_header_1+size_of_header_2;

/*************************************to check if vfs exists*************************************************************/
     
       fdi=fopen(labels,"rb+");
         if(fdi==NULL)
   {
   strcpy(error,"exportfile_FAILURE");
    strcat(error,ERR_VFS_EXPORTFILE_04);
    printf(ERR_VFS_EXPORTFILE_04);
   return error;
   }
/*************************************to check if source file exists*************************************************************/
	b_node *tmp=bst_search(bst_root,file_path);
	printf("\n");
         if(tmp==NULL){
    strcpy(error,"export_FAILURE ");
    strcat(error,ERR_VFS_EXPORTFILE_01);
    printf(ERR_VFS_EXPORTFILE_01);
   return error;
   }
	
/**********************************************to check if it is a text_file****************************************************/

	strcpy(abspath,file_path);

	name=strtok(abspath,"/");

	while(name!=NULL){
		strcpy(fname,name);
		name=strtok(NULL,"/");
	}

       

        int block_num=tmp->file_desc->blockx;
if(tmp->file_desc->is_dir==1)
{
                       strcpy(error,"export_FAILURE");
    			strcat(error,ERR_VFS_EXPORTFILE_03);
    			printf(ERR_VFS_EXPORTFILE_03);
   			return error;
 

}




	fseek(fdi,total_size+(1024*(block_num)),SEEK_SET);
        fread(&blk,1024,1,fdi);
       
	fp=fopen(output_file,"wb+");
	if(fp==NULL)
		{
                        strcpy(error,"export_FAILURE ");
    			strcat(error,ERR_VFS_EXPORTFILE_02);
    			printf(ERR_VFS_EXPORTFILE_02);
   			return error;
   
			}
	else
	{ 
      int sz=tmp->file_desc->size;
           int g;
           for(g=0;g<sz;g++){
	   fwrite(&(blk.bsize[g]),1024,1,fp);}
	   fclose(fp);
	}
 
	

		 memset(blk.bsize, 0, 1024);
    strcpy(error,"export_SUCCESS");
    printf(error);
   return error;
 
}

